<?php
namespace Work\Controller;

use Common\Library\CompanyDao;
use Common\Library\CompanyModel;
use Common\Library\NewsClassifyDao;
use Common\Library\NewsClassifyModel;
use Common\Library\SiteDao;
use Think\Controller;

/**
 * 主页的控制器
 * @package Work\Controller
 */
class IndexController extends Controller{

    /*
     * 首页展示，与当前登录用户名显示
     * */
    public function index(){
        $name = $_SESSION['cp_user'];
        $id = $_SESSION['cp_id'];
        $this->assign('name', $name);
        $this->display('index');
    }
    /**
     *头部
     */
    public function header() {
       echo "welcome to DouPHP";
    }


    /**
     * 添加新闻分类
     */
    public function add_newsclassify() {
        // 当前用户对象
        $company = CompanyDao::FetchCurrent();
        // 处理方法
        if (IS_POST) {
            // 获取现有分类对象
            if(isset($_GET['id'])){
                $newsClassify = NewsClassifyDao::FetchByID($_GET['id']);
                if($newsClassify->getSite()->id != $company->getSite()->id)
                    $this->error('无权限修改该分类信息！');
            }
            // 创建一个新分类对象
            else {
                $newsClassify = new NewsClassifyModel();
            }
            // 接收数据给新对象赋值
            $newsClassify->name = I('post.nc_name');
            $newsClassify->detail = I('post.nc_detail');
            // 保存新对象
            if(isset($_GET['id'])){
                NewsClassifyDao::Save($newsClassify);
            }
            else {
                NewsClassifyDao::Create($newsClassify,$company->getSite());
            }
            redirect('/Index/show_newsclassify');
        } else {
            if(isset($_GET['id'])){
                $newsClassify = NewsClassifyDao::FetchByID($_GET['id']);
                if($newsClassify->getSite()->id != $company->getSite()->id){
                    $this->error('无权限修改该分类信息！');
                }
                $this->assign('data', $newsClassify);
            }
            $this->display('/Index/add_newsclassify');
        }
    }

    /**
     * 查看新闻分类
     */
    public function show_newsclassify() {
        $company = CompanyDao::FetchCurrent();
        $site = $company->getSite();
        $newsClassify = $site->getNewsClassify();
        //对数据进行赋值
        $this->assign('nc_data',$newsClassify);
        //显示出页面
        $this->display();
    }

    /**
     * 删除一条新闻分类记录
     */
    public function del_newsclassify()
    {
        $id = $_SESSION['cp_id'];
        $site = D('site');
        $siteinfo = $site->where(['cp_id'=>$id])->select();
        $si_id = $siteinfo[0]['si_id'];
        $User = D('newsclassify');
        $id   = $_GET['id'];
        $User->where(['nc_id'=>$id])->where(['si_id'=>$si_id])->delete();
        redirect('/index/show_newsclassify');
    }

    /**
     * 添加新闻列表内容
     */
    public function add_newsarticle() {
        //在下拉框中显示出分类的名字
        $id = $_SESSION['cp_id'];
        $site = D('site');
        $siteinfo = $site->where(['cp_id'=>$id])->select();
        $si_id = $siteinfo[0]['si_id'];
        $result = D('newsclassify');
        $data1  = $result->where(['si_id'=>$si_id])->select();
        $this->assign('nc_data',$data1);

        if (IS_POST) {
//            $NewsClassify = D('newsclassify');
            $nc_id            = I('post.nc_id');
//            $classifyInfo = $NewsClassify->where(['nc_id'=>$nc_id])->select();
//
//            if(empty($classifyInfo)){
//                // todo
//                $this->error('分类不存在','/index/show_newsclassify');
//            }
//            if($classifyInfo[0]['si_id']!= $_SESSION['cp_id'] ){
                $User = D('newsarticle');
                $data['ne_title'] = I('post.ne_title');
                $data['ne_d']     = I('post.ne_d');
                $data['nc_id'] = $nc_id;
                $data['ne_time']  = date('Y-m-d,h:s:m');
                $User->data($data)->add();
                redirect('/Index/show_newsarticle');
//            }
        } else {
            $this->display('/Index/add_newsarticle');
        }
    }

    /**
     *查看新闻列表内容
     */
    public function show_newsarticle() {
        $id   = $_SESSION['cp_id'];
        $site = M('site');
        $siteinfo = $site->where(['cp_id' => $id])->select();
        $si_id = $siteinfo[0]['si_id'];

        $newclassify     = D('newsclassify');
        $newclassifyinfo = $newclassify->where(['si_id' => $si_id])->select();
        $nc_id_list = array();
        foreach ($newclassifyinfo as $value){
            $nc_id_list[] = $value['nc_id'];
        }
        $nc_id = implode(',', $nc_id_list);

        $result = D('newsarticle');
        $map['nc_id'] = array('IN',$nc_id);
        $data = $result->where($map)->select();
        //总共数据
        $count  = $result->where($map)->count();
        $this->assign('count',$count);
        $this->assign('ne_data',$data);
        $this->display();
    }

<<<<<<< HEAD
    public function set_page(){
        $this->display('set_page');
    }

    public function add_page(){
        $this->display('add_page');
    }

}
=======
    /**
     * 删除一个新闻列表记录
     */
    public function del_newsarticle() {
        $User = D('newsarticle');
        $id = $_GET['id'];
        $User->where(['ne_id' => $id])->delete();
        redirect('/index/show_newsarticle');
    }

    /**
     * 添加广告分类
     */
    public function add_adposition() {
        $id = $_SESSION['cp_id'];
        $site = D('site');
        $siteinfo = $site->where(['si_id'=>$id])->select();
        $si_id = $siteinfo[0]['si_id'];
        if (IS_POST) {
            $result = D('adposition');
            $data['ap_name']    = I('post.ap_name');
            $data['ap_detail']  = I('post.ap_detail');
            $data['si_id'] = $si_id;
            $result->data($data)->add();
            redirect('/Index/show_adposition');
        } else {
            $this->display('/Index/add_adposition');
        }
    }

    /**
     * 查看广告分类
     */
    public function show_adposition() {
        $id = $_SESSION['cp_id'];
        $site = D('site');
        $siteinfo = $site->where(['si_id'=>$id])->select();
        $si_id = $siteinfo[0]['si_id'];
        $result = D('adposition');
        //查找数据
        $data  = $result->where(['si_id'=>$si_id])->select();
//        var_dump($data);die;
        //对数据进行赋值
        $this->assign('ap_data',$data);
        //显示出页面
        $this->display();
    }

    /**
     * 删除一条新闻分类记录
     */
    public function del_adposition()
    {
        $User = D('adposition');
        $id   = $_GET['id'];
        $User->where(['ap_id'=>$id])->delete();
        redirect('/index/show_adposition');
    }
}
>>>>>>> 7cd9864f81826264a56a445aa904a52a979c968f
